<template>
  <div>
    <div class="container">
      <h1>{{ article.title }}</h1>
      <p v-show="article.channel_id">
        <span>{{ article.created_at }}</span>
        <span>lmb</span>
        <span>{{ fromArticle(article.channel_id) }}</span>
        {{ article.browse }}
      </p>
      <div
        class="content"
        v-html="article.content"
        @click="previewPicture($event)"
      ></div>
      <img
        v-for="(i, index) in imgs"
        :key="index"
        :src="images(i)"
        alt=""
        @click="large(images(i))"
      />
    </div>
    <Masks
      ref="childRef"
      :bigPicture="bigPicture"
      :isMask="isMask"
      @changeMaskState="changeMaskState"
    />
  </div>
</template>

<script>
import { getArticle } from '../api/article'
import Masks from '@/components/Masks.vue'
export default {
  components: {
    Masks
  },
  data() {
    return {
      article: {},
      baseURL: this.baseURL,
      imgs: [],
      bigPicture: '',
      isMask: false
    }
  },
  async created() {
    if (this.$route.query.id) {
      const res = await getArticle({ id: this.$route.query.id })
      this.article = res.data.data
      // 替换v-html中的图片属性img设置样式
      // this.article.content = this.article.content.replace(
      //   /<img/g,
      //   "<img style='max-width:80%;height:auto;margin:5px'"
      // );
      this.imgs = this.article.image && this.article.image.split(',')
      console.log(this.imgs)
    }
    // function parseUrl() {
    //   var url = location.href;
    //   var i = url.indexOf("?");
    //   if (i === -1) return;
    //   var querystr = url.substr(i + 1);
    //   var arr1 = querystr.split("&");
    //   var arr2 = new Object();
    //   for (i in arr1) {
    //     var ta = arr1[i].split("=");
    //     arr2[ta[0]] = ta[1];
    //   }
    //   return arr2;
    // }
    // var v = parseUrl();
    // alert(v["id"]);
  },
  computed: {
    images: function () {
      return function (img) {
        return img === '' ? '' : `${this.baseURL}/uploads/${img}`
      }
    },
    fromArticle: function () {
      return function (v) {
        switch (v) {
          case 1:
            return '博客日记'
          case 2:
            return '闲言碎语'
          case 3:
            return '语录簿'
          case 4:
            return '相册'
          case 5:
            return '前端分享'
          default:
            return '心情'
        }
      }
    }
  },
  methods: {
    large(v) {
      this.bigPicture = v
      this.isMask = true
      this.$refs.childRef.enlarge()
    },
    changeMaskState() {
      this.isMask = false
    },
    previewPicture(e) {
      console.log(e)
      if (e.target.tagName === 'IMG') {
        // this.picturePreview.src = e.target.src;
        // this.picturePreview.visible = true;
        this.bigPicture = e.target.src
        this.isMask = true
        this.$refs.childRef.enlarge()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@media only screen and (max-width: 480px) {
  body .container {
    padding: 20px 10px;
    h1 {
      font-size: 18px;
    }
    p {
      font-size: 13px;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      span {
        margin-right: 10px;
      }
    }
    .content {
      ::v-deep p {
        padding: 2px 0;
      }
      ::v-deep li {
        margin-left: 10px;
      }
    }
    img {
      margin: 5px;
      width: 96%;
      box-sizing: border-box;
      height: 150px;
    }
  }
}

.container {
  background: #fff;
  padding: 20px;
  border-radius: 3px;
  word-wrap: break-word;
  word-break: break-all;
  h1 {
    font-size: 22px;
    margin-bottom: 20px;
  }
  p {
    margin-bottom: 20px;
    span {
      margin-right: 15px;
      position: relative;
      padding-left: 20px;
      line-height: 20px;
      cursor: pointer;
    }
    span:nth-child(1) {
      background: url(../assets/icon.png) no-repeat 0 2px;
      background-size: 18px;
    }
    span:nth-child(2) {
      background: url(../assets/icon.png) no-repeat 0 -16px;
      background-size: 18px;
    }
    span:nth-child(3) {
      background: url(../assets/icon.png) no-repeat 0 -34px;
      background-size: 18px;
      color: #518f97;
    }
    span:nth-child(n):before {
      position: absolute;
      content: '';
      width: 20px;
      height: 20px;
      left: 0;
      top: 0;
    }
  }
  .content {
    margin: 20px 0 10px;
    ::v-deep img {
      margin: 10px 0;
      box-sizing: border-box;
      width: 100%;
      height: 250px;
      object-fit: cover;
    }
    ::v-deep p {
      padding: 5px 0;
    }
    ::v-deep li {
      margin-left: 20px;
    }
  }
  img {
    box-sizing: border-box;
    width: 100%;
    height: 250px;
    object-fit: cover;
  }
}
</style>
